set more off
graph drop _all

	
use Data/Estimation_sample.dta, clear
	
	gen nexis_buildings  = ROOFME/PROOFME
		
		
	gen roofratio = ROOFME/ROOFTI
	gen roofratiobuildings = roofratio*nexis_buildings
		
	gen roofrelativeIV = (roofratio*nexis_buildings/[moduleprice])/1000

	
 
	* 3 quarters is nine months 
	local length = 3
		 
	forval t = 0(1)`length' {
	* `t' < 0 indicates in low subsidy period relative to the event

		gen event_after`t' =  SHCP_end ==`t' |  PFIT_starttime==`t' | PFIT_endtime==`t' | ///
		 SRES1_end ==`t' | SRES2_end==`t' | SRES_TFIT_endtime==`t' | FFIT_endtime==`t'
		 
		 
		 gen event_before`t' =  SHCP_end ==-`t'  | PFIT_starttime==-`t' | PFIT_endtime==-`t' | ///
		 SRES1_end ==-`t' | SRES2_end==-`t' | SRES_TFIT_endtime==-`t' | FFIT_endtime==-`t'
		
		gen event_after_new`t' = event_after`t'*cum
		gen event_before_new`t' = event_before`t'*cum
		
		gen event_after_high`t' = event_after`t'*roofrelativeIV
		gen event_before_high`t' = event_before`t'*roofrelativeIV
		
		
		drop event_after`t'
		drop event_before`t'
		
		
		
		}
			
	
	label variable event_before_new3  "Event Period -3"
	label variable event_before_new2  "Event Period -2"
	label variable event_before_new1  "Event Period -1"
	label variable event_after_new0   "Event Period "
	label variable event_after_new1   "Event Period +1"
	label variable event_after_new2   "Event Period +2"
	label variable event_after_new3   "Event Period +3"


	drop  event_before_high0  event_before_new0 
	
	// event_before_high1 event_before_new1
	*drop  event_before_2high0   // event_before_high1 event_before_new1
	

	
	
	merge m:1 postcode using Data/Postcode_matching.dta
	drop if _merge ==2
	drop _merge 

	
	
	merge m:1 postcode using Data/SA3postcode.dta
	
	drop if _merge ==2
	drop _merge
	
	merge m:1 SA3_CODE16 using "data/maps/sa3" 

	drop if _merge ==2
	drop _merge
	
	*  melbourne
	gen Melbourne = 1 if SA4_CODE=="213" | SA4_CODE =="210" | SA4_CODE=="209" | SA4_CODE =="207" | SA4_CODE=="211"  | SA4_CODE=="208" | 	SA4_CODE=="206" | SA4_CODE=="212"
	replace Melbourne =0 if Melbourne ==.`'
				

	gen notinner25 = (Melbourne ==1 )*(GPO_distance>2.5)
	gen notinner5 = (Melbourne ==1 )*(GPO_distance>5)
	gen notinner75 = (Melbourne ==1 )*(GPO_distance>7.5)
	gen notinner10 = (Melbourne ==1 )*(GPO_distance>10)
	
	***********************
	****** WEIGHTING BY CUSTOMER *****
	***********************
	
	
******
	xi: xtivreg2 green_p event_before_new3 event_before_new2 event_before_new1  event_after_new0 event_after_new1  event_after_new2 event_after_new3  ///
	  i.quarter  [aweight =NCustomer] if  Melbourne==1 ,  fe cluster(postcode)

	  
	  	
	quietly summ green_plan [aweight =NCust	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	  

	 outreg2 using Analysis/Motivation_suburbs, bdec(3) se  nocons label  ctitle("All suburbs")  ///
		keep(event* cum  median_*) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, `meandepvar') tex(fragment pretty) nonotes append

	eststo model2c 

	coefplot (model2c, keep(event_before_new3  event_before_new2  event_before_new1) mcolor(blue) ciopts(lcolor(blue)) msymbol(o)) ///
	 (model2c, keep(event_after_new0 ) mcolor(gs0) ciopts(lcolor(gso)) msymbol(o)) ///
	(model2c, keep( event_after_new1 event_after_new2 event_after_new3 ) mcolor(red) ciopts(lcolor(red)) msymbol(o)), vertical ///
	 relocate(event_before_new3 = -2.84 event_before_new2 = -1.84 event_before_new1 = -0.84 event_after_new0 = -0 ///
	event_after_new1 =0.84  event_after_new2=1.84 event_after_new3=2.84 )   legend(off) yline(0) ///
	xtitle("Event time") xlabel(-3(1)3)  xline(0, lcolor(gs10) lpattern(dash) lwidth(vthin)) yscale(range(-0.025 0.025)) ylabel(-0.02(0.01)0.02) 


graph export Analysis/Motivation_Melbsuburbs.png, replace
	
	xi: xtivreg2 green_p event_before_new3 event_before_new2 event_before_new1  event_after_new0 event_after_new1  event_after_new2 event_after_new3  ///
	  i.quarter  [aweight =NCustomer] if GPO_distance  <= 5 & Melbourne==1 ,  fe cluster(postcode)

	  
	  	
	quietly summ green_plan [aweight =NCust	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	  

	 outreg2 using Analysis/Motivation_suburbs, bdec(3) se  nocons label  ctitle("Inner suburbs")  ///
		keep(event* cum  median_*) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, `meandepvar') tex(fragment pretty) nonotes append

	eststo model2c 

	coefplot (model2c, keep(event_before_new3  event_before_new2  event_before_new1) mcolor(blue) ciopts(lcolor(blue)) msymbol(o)) ///
	 (model2c, keep(event_after_new0 ) mcolor(gs0) ciopts(lcolor(gso)) msymbol(o)) ///
	(model2c, keep( event_after_new1 event_after_new2 event_after_new3 ) mcolor(red) ciopts(lcolor(red)) msymbol(o)), vertical ///
	 relocate(event_before_new3 = -2.84 event_before_new2 = -1.84 event_before_new1 = -0.84 event_after_new0 = -0 ///
	event_after_new1 =0.84  event_after_new2=1.84 event_after_new3=2.84 )   legend(off) yline(0) ///
	xtitle("Event time") xlabel(-3(1)3)  xline(0, lcolor(gs10) lpattern(dash) lwidth(vthin)) yscale(range(-0.025 0.025)) ylabel(#5) 


graph export Analysis/Motivation_innersuburbs.png, replace

	  
	xi: xtivreg2 green_p event_before_new3 event_before_new2 event_before_new1  event_after_new0 event_after_new1  event_after_new2 event_after_new3  ///
	  i.quarter  [aweight =NCustomer] if GPO_distance > 5 & GPO_distance <= 20 & Melbourne==1 ,  fe cluster(postcode)

	  
	  	
	quietly summ green_plan [aweight =NCust	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	  

	 outreg2 using Analysis/Motivation_suburbs, bdec(3) se  nocons label  ctitle("Melbourne")  ///
		keep(event* cum  median_*) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, `meandepvar') tex(fragment pretty) nonotes append

	eststo model2c 

	coefplot (model2c, keep(event_before_new3  event_before_new2  event_before_new1) mcolor(blue) ciopts(lcolor(blue)) msymbol(o)) ///
	 (model2c, keep(event_after_new0 ) mcolor(gs0) ciopts(lcolor(gso)) msymbol(o)) ///
	(model2c, keep( event_after_new1 event_after_new2 event_after_new3 ) mcolor(red) ciopts(lcolor(red)) msymbol(o)), vertical ///
	 relocate(event_before_new3 = -2.84 event_before_new2 = -1.84 event_before_new1 = -0.84 event_after_new0 = -0 ///
	event_after_new1 =0.84  event_after_new2=1.84 event_after_new3=2.84 )   legend(off) yline(0) ///
	xtitle("Event time") xlabel(-3(1)3)  xline(0, lcolor(gs10) lpattern(dash) lwidth(vthin)) yscale(range(-0.025 0.025)) ylabel(-0.02(0.01)0.02) 


graph export Analysis/Motivation_middlesuburbs.png, replace

	  

	xi: xtivreg2 green_p event_before_new3 event_before_new2 event_before_new1  event_after_new0 event_after_new1  event_after_new2 event_after_new3  ///
	  i.quarter  [aweight =NCustomer] if GPO_distance >= 20 & Melbourne==1 ,  fe cluster(postcode)

	  
	  	
	quietly summ green_plan [aweight =NCust	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	  

	 outreg2 using Analysis/Motivation_suburbs, bdec(3) se  nocons label  ctitle("POuter suburbs")  ///
		keep(event* cum  median_*) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, `meandepvar') tex(fragment pretty) nonotes append

	eststo model2c 

	coefplot (model2c, keep(event_before_new3  event_before_new2  event_before_new1) mcolor(blue) ciopts(lcolor(blue)) msymbol(o)) ///
	 (model2c, keep(event_after_new0 ) mcolor(gs0) ciopts(lcolor(gso)) msymbol(o)) ///
	(model2c, keep( event_after_new1 event_after_new2 event_after_new3 ) mcolor(red) ciopts(lcolor(red)) msymbol(o)), vertical ///
	 relocate(event_before_new3 = -2.84 event_before_new2 = -1.84 event_before_new1 = -0.84 event_after_new0 = -0 ///
	event_after_new1 =0.84  event_after_new2=1.84 event_after_new3=2.84 )   legend(off) yline(0) ///
	xtitle("Event time") xlabel(-3(1)3)  xline(0, lcolor(gs10) lpattern(dash) lwidth(vthin)) yscale(range(-0.025 0.025)) ylabel(-0.02(0.01)0.02) 


graph export Analysis/Motivation_outersuburbs.png, replace

	  
	
	  
